﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///OAuth应用
    /// </summary>
    public partial class oauth_app
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public oauth_app()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_oauth_app>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_oauth_app model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_oauth_app model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_oauth_app>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_oauth_app GetModel(int id)
        {
            return data.Get<Entitys.dt_oauth_app>(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_oauth_app> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_oauth_app>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_oauth_app> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_oauth_app>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_oauth_app GetModel(string api_path)
        {
            return data.Get<Entitys.dt_oauth_app>(x=>x.api_path==api_path);
        }

        /// <summary>
        /// 获取站点未添加数据
        /// </summary>
        public List<Entitys.dt_oauth_app> GetList(int site_id, int oauth_id)
        {
            List<Entitys.dt_site_oauth> siteList = data.List<Entitys.dt_site_oauth>(x => x.site_id == site_id);
            List<int> sites = new List<int>();
            foreach (Entitys.dt_site_oauth p in siteList)
            {
                sites.Add(Utils.ObjToInt(p.oauth_id));
            }

            return data.List<Entitys.dt_oauth_app>(x => x.is_lock == 0 && !sites.Contains(x.id), x => x.sort_id, SqlSugar.OrderByType.Asc);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(Expression<Func<Entitys.dt_oauth_app, Entitys.dt_oauth_app>> expression, Expression<Func<Entitys.dt_oauth_app, bool>> whereExpression)
        {
            return data.UpdateColumns<Entitys.dt_oauth_app>(expression, whereExpression);
        }
        #endregion
    }
}